package com.stylefeng.guns.common.persistence.service.impl;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.common.annotion.handle.PointComponent;
import com.stylefeng.guns.common.constant.Const;
import com.stylefeng.guns.common.persistence.model.NetworkTrainPoints;
import com.stylefeng.guns.core.util.ReflectUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class PointServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> {

    @Autowired
    private PointComponent pointComponent;

    @Override
    public boolean deleteById(Serializable id) {
        Object old=super.selectById(id);
        boolean success= super.deleteById(id);
        if (success) {
            pointComponent.pointTypeHandle(null,old, Const.OPERATION_DELETE);
        }
        return success;
    }

    @Override
    public boolean deleteBatchIds(List<? extends Serializable> idList) {
        List<T> oldList = super.selectBatchIds(idList);
        boolean success = super.deleteBatchIds(idList);
        if (success) {
            for (Object obj : oldList) {
                pointComponent.pointTypeHandle(null, obj, Const.OPERATION_DELETE);
            }
        }
        return success;
    }

    @Override
    public boolean deleteByMap(Map columnMap) {
        List<T> oldList=super.selectByMap(columnMap);
        boolean success= super.deleteByMap(columnMap);
        if (success) {
            for (Object obj:oldList) {
                pointComponent.pointTypeHandle(null, obj, Const.OPERATION_DELETE);
            }
        }
        return success;
    }

    @Override
    public boolean delete(Wrapper<T> wrapper) {
        Object obj=super.selectObj(wrapper);
        boolean success= super.delete(wrapper);
        if (success) {
            pointComponent.pointTypeHandle(null,obj, Const.OPERATION_DELETE);
        }
        return success;
    }

    @Override
    public boolean update(T entity, Wrapper<T> wrapper) {
        Object old=super.selectObj(wrapper);
        boolean success=super.update(entity, wrapper);
        if (success) {
            pointComponent.pointTypeHandle(entity,old,Const.OPERATION_UPDATE);
        }
        return success;
    }

    @Override
    public boolean updateById(T entity) {
        Object old=super.selectById((Serializable)ReflectUtil.getId(entity));
        boolean success= super.updateById(entity);
        if (success) {
            pointComponent.pointTypeHandle(entity,old,Const.OPERATION_UPDATE);
        }
        return success;
    }

    @Override
    public boolean insert(T entity) {
        boolean success= super.insert(entity);
        if (success) {
            pointComponent.pointTypeHandle(entity,null,Const.OPERATION_ADD);
        }
        return success;
    }

    @Override
    public boolean insertBatch(List<T> entityList) {
        boolean success=super.insertBatch(entityList);
        if (success) {
            for (T t:entityList) {
                pointComponent.pointTypeHandle(t, null, Const.OPERATION_ADD);
            }
        }
        return success;
    }

}